﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Atlantyda
{
    public class MapField : Field
    {
        public static readonly Color TYPE_ENERGY = Color.Blue;
        public static readonly Color TYPE_WALL = Color.Black;
        public static readonly Color TYPE_DEFAULT = Color.FromArgb(142, 180, 0);
        private Color color = TYPE_DEFAULT;
        private System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
        private Person person;
        private bool visited;

        public System.Windows.Forms.Panel Panel
        {
            get { return this.panel; }
            set { this.panel = value; }
        }
        public bool Visited
        {
            get { return this.visited; }
            set { this.visited = value; }
        }

        public Person Person
        {
            get { return this.person; }
            set { this.person = value; }
        }

        public Color Color
        {
            get { return this.color; }
            set { this.color = value; }
        }

        public MapField(int x, int y, int height, Color type, int energy)
        {
            this.CoordianteX = x;
            this.CoordianteY = y;
            this.Height = height;
            if (type == MapField.TYPE_DEFAULT)
            {
                this.Color = this.GenerateColorByHeight();
            }
            else
            {
                this.Color = type;
            }
            this.Energy = energy;
            this.Panel.Name = "panel" + this.CoordianteX + "_" + this.CoordianteY;
            Agent.MainForm.AgentPanel.InvokeEx(panel => panel.Controls.Add(this.Panel));
        }

        public Color GenerateColorByHeight()
        {
            int red = 0;
            int green = 0;
            if (this.Height < 50 && this.Height > 0)
            {
                red = Convert.ToInt32(Math.Floor(4.6 * this.Height));
                green = Convert.ToInt32(Math.Floor(2.6 * this.Height + 100));
            }
            else if (this.Height >= 50)
            {
                red = Convert.ToInt32(Math.Floor(-2.6 * (this.Height - 50) + 230));
                green = Convert.ToInt32(Math.Floor(-4.6 * (this.Height - 50) + 230));
            }
            return Color.FromArgb(red, green, 0);
        }

        public string DoubleToHex(double value, int maxDecimals)
        {
            string result = string.Empty;
            if (value < 0)
            {
                result += "-";
                value = -value;
            }
            if (value > ulong.MaxValue)
            {
                result += double.PositiveInfinity.ToString();
                return result;
            }
            ulong trunc = (ulong)value;
            result += trunc.ToString("X");
            value -= trunc;
            if (value == 0)
            {
                return result;
            }
            result += ".";
            byte hexdigit;
            while ((value != 0) && (maxDecimals != 0))
            {
                value *= 16;
                hexdigit = (byte)value;
                result += hexdigit.ToString("X");
                value -= hexdigit;
                maxDecimals--;
            }
            return result;
        }
    }
}
